import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { InfoStoreApi } from '#/api/spa/store';

import { getInfoGroupPage } from '#/api/spa/group';
import { getSiteList } from '#/api/spa/site';


import { getDictOptions, getRangePickerDefaultProps } from '#/utils';
import { DICT_SPA_TYPE } from '#/utils/dictSpa';

/** 站点信息类型 */
interface SiteInfo {
  siteId: number;
  siteName: string;
  siteDomain: string;
  groupId: number;
  groupName: string;
  status: number;
  statusName: string;
}

/** 获取集团选项 */
function getGroupOptions() {
  // 使用API选择器的方式，让表单组件自动处理异步加载
  return {
    api: getInfoGroupPage,
    resultField: 'list',
    labelField: 'groupName',
    valueField: 'groupName',
  };
}

/** 获取站点选项 */
function getSiteOptions() {
  // 使用API选择器的方式，让表单组件自动处理异步加载
  return {
    api: getSiteList,
    resultField: 'data',
    labelField: 'site_name',
    valueField: 'site_id',
  };
}

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'siteInfo',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'groupName',
      label: '所属集团',
      rules: 'required',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择所属集团',
        ...getGroupOptions(),
        showSearch: true,
        filterOption: (input: string, option: any) => {
          return option.label.toLowerCase().includes(input.toLowerCase());
        },
      },
      dependencies: {
        triggerFields: ['id'],
        componentProps: (values: any) => {
          // 如果是编辑模式（有id），则禁用选择
          return {
            disabled: !!values.id,
          };
        },
      },
    },
    {
      fieldName: 'storeCode',
      label: '门店编码',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入门店编码',
      },
      dependencies: {
        triggerFields: ['id'],
        componentProps: (values: any) => {
          // 如果是编辑模式（有id），则禁用输入
          return {
            disabled: !!values.id,
          };
        },
      },
    },
    {
      fieldName: 'storeName',
      label: '门店名称',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入门店名称',
      },
    },
    {
      fieldName: 'siteIds',
      label: '绑定站点',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择绑定站点',
        mode: 'multiple',
        ...getSiteOptions(),
        showSearch: true,
        filterOption: (input: string, option: any) => {
          return option.label.toLowerCase().includes(input.toLowerCase());
        },
      },
      dependencies: {
        triggerFields: ['siteIds'],
      },
    },
    // {
    //   fieldName: 'parentStoreId',
    //   label: '父门店ID',
    //   component: 'Input',
    //   componentProps: {
    //     placeholder: '请输入父门店ID',
    //   },
    // },
    {
      fieldName: 'storeType',
      label: '门店类型',
      rules: 'required',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_SPA_TYPE.SPA_STORE_TYPE, 'string'),
        placeholder: '请选择门店类型',
      },
    }, {
      fieldName: 'status',
      label: '状态',
      rules: 'required',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_SPA_TYPE.SPA_Store_Status, 'string'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'contactPerson',
      label: '联系人',
      component: 'Input',
      componentProps: {
        placeholder: '请输入联系人',
      },
    },
    {
      fieldName: 'contactPhone',
      label: '联系电话',
      component: 'Input',
      componentProps: {
        placeholder: '请输入联系电话',
      },
    },
    {
      fieldName: 'contactEmail',
      label: '联系邮箱',
      component: 'Input',
      componentProps: {
        placeholder: '请输入联系邮箱',
      },
    },
    {
      fieldName: 'address',
      label: '地址',
      component: 'Input',
      componentProps: {
        placeholder: '请输入地址',
      },
    },
    // {
    //   fieldName: 'longitude',
    //   label: '经度',
    //   component: 'Input',
    //   componentProps: {
    //     placeholder: '请输入经度',
    //   },
    // },
    // {
    //   fieldName: 'latitude',
    //   label: '纬度',
    //   component: 'Input',
    //   componentProps: {
    //     placeholder: '请输入纬度',
    //   },
    // },
    {
      fieldName: 'businessHours',
      label: '营业时间',
      component: 'Input',
      componentProps: {
        placeholder: '请输入营业时间',
      },
    },

    {
      fieldName: 'openTime',
      label: '开业时间',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'config',
      label: '门店配置',
      component: 'Input',
      componentProps: {
        placeholder: '请输入门店配置',
      },
    },
    {
      fieldName: 'iotConfig',
      label: 'IoT设备配置',
      component: 'Input',
      componentProps: {
        placeholder: '请输入IoT设备配置',
      },
    },
    {
      fieldName: 'orgId',
      label: '组织ID',
      component: 'Input',
      componentProps: {
        placeholder: '请输入组织ID',
      },
    },
    {
      fieldName: 'businessLicense',
      label: '营业执照号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入营业执照号',
      },
    },
    {
      fieldName: 'storeManager',
      label: '店长',
      component: 'Input',
      componentProps: {
        placeholder: '请输入店长',
      },
    },
    {
      fieldName: 'city',
      label: '城市',
      component: 'Input',
      componentProps: {
        placeholder: '请输入城市',
      },
    },
    {
      fieldName: 'district',
      label: '区县',
      component: 'Input',
      componentProps: {
        placeholder: '请输入区县',
      },
    },
    {
      fieldName: 'handCardCount',
      label: '手牌数量',
      component: 'Input',
      componentProps: {
        placeholder: '请输入手牌数量',
      },
    },
    {
      fieldName: 'roomCount',
      label: '房间数量',
      component: 'Input',
      componentProps: {
        placeholder: '请输入房间数量',
      },
    },
    {
      fieldName: 'technicianCount',
      label: '技师数量',
      component: 'Input',
      componentProps: {
        placeholder: '请输入技师数量',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'groupName',
      label: '所属集团',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        placeholder: '请选择所属集团',
        ...getGroupOptions(),
        showSearch: true,
        filterOption: (input: string, option: any) => {
          return option.label.toLowerCase().includes(input.toLowerCase());
        },
      },
    },
    {
      fieldName: 'storeCode',
      label: '门店编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入门店编码',
      },
    },
    {
      fieldName: 'storeName',
      label: '门店名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入门店名称',
      },
    },
    {
      fieldName: 'siteIds',
      label: '绑定站点',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        placeholder: '请选择绑定站点',
        mode: 'multiple',
        ...getSiteOptions(),
        showSearch: true,
        filterOption: (input: string, option: any) => {
          return option.label.toLowerCase().includes(input.toLowerCase());
        },
      },
    },
    // {
    //   fieldName: 'parentStoreId',
    //   label: '父门店ID',
    //   component: 'Input',
    //   componentProps: {
    //     allowClear: true,
    //     placeholder: '请输入父门店ID',
    //   },
    // },
    {
      fieldName: 'storeType',
      label: '门店类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_SPA_TYPE.SPA_STORE_TYPE, 'string'),
        placeholder: '请选择门店类型',
      },
    }, {
      fieldName: 'status',
      label: '状态',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入状态',
      },
    },

    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useCheckoutGridColumns(): VxeTableGridOptions<InfoStoreApi.InfoStore>['columns'] {
  return [
    {
      field: 'storeCode',
      title: '门店编码',
      minWidth: 120,
    },
    {
      field: 'storeName',
      title: '门店名称',
      minWidth: 150,
    },
    {
      field: 'groupName',
      title: '所属集团',
      minWidth: 120,
    },
    {
      field: 'contactPerson',
      title: '联系人',
      minWidth: 100,
    },
    {
      field: 'contactPhone',
      title: '联系电话',
      minWidth: 120,
    },
    {
      field: 'address',
      title: '地址',
      minWidth: 200,
    },
    {
      field: 'siteInfo',
      title: '绑定站点',
      minWidth: 300,
      slots: { default: 'siteInfo' },
    },
    {
      title: '操作',
      width: 150,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<InfoStoreApi.InfoStore>['columns'] {
  return [
    // {
    //   field: 'id',
    //   title: '门店ID',
    //   minWidth: 120,
    // },
    // {
    //   field: 'groupId',
    //   title: '所属集团ID',
    //   minWidth: 120,
    // },
    {
      field: 'storeCode',
      title: '门店编码',
      minWidth: 120,
    },
    {
      field: 'storeName',
      title: '门店名称',
      minWidth: 120,
    },
    {
      field: 'siteInfo',
      title: '绑定站点',
      minWidth: 300,
      slots: { default: 'siteInfo' },
    },
    {
      field: 'groupCode',
      title: '集团编码',
      minWidth: 120,
    },
    {
      field: 'groupName',
      title: '集团名称',
      minWidth: 120,
    },
    // {
    //   field: 'parentStoreId',
    //   title: '父门店ID',
    //   minWidth: 120,
    // },
    {
      field: 'storeType',
      title: '门店类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_SPA_TYPE.SPA_STORE_TYPE},
      },
    },{
      field: 'status',
      title: '状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_SPA_TYPE.SPA_Store_Status},
      },
    },
    {
      field: 'contactPerson',
      title: '联系人',
      minWidth: 120,
    },
    {
      field: 'contactPhone',
      title: '联系电话',
      minWidth: 120,
    },
    {
      field: 'contactEmail',
      title: '联系邮箱',
      minWidth: 120,
    },
    {
      field: 'address',
      title: '地址',
      minWidth: 120,
    },
    {
      field: 'longitude',
      title: '经度',
      minWidth: 120,
    },
    {
      field: 'latitude',
      title: '纬度',
      minWidth: 120,
    },
    {
      field: 'businessHours',
      title: '营业时间',
      minWidth: 120,
    },

    {
      field: 'openTime',
      title: '开业时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'config',
      title: '门店配置',
      minWidth: 120,
    },
    {
      field: 'iotConfig',
      title: 'IoT设备配置',
      minWidth: 120,
    },
    // {
    //   field: 'orgId',
    //   title: '组织ID',
    //   minWidth: 120,
    // },
    {
      field: 'businessLicense',
      title: '营业执照号',
      minWidth: 120,
    },
    {
      field: 'storeManager',
      title: '店长',
      minWidth: 120,
    },
    {
      field: 'city',
      title: '城市',
      minWidth: 120,
    },
    {
      field: 'district',
      title: '区县',
      minWidth: 120,
    },
    // {
    //   field: 'createTime',
    //   title: '创建时间',
    //   minWidth: 120,
    //   formatter: 'formatDateTime',
    // },
    {
      field: 'handCardCount',
      title: '手牌数量',
      minWidth: 120,
    },
    {
      field: 'roomCount',
      title: '房间数量',
      minWidth: 120,
    },
    {
      field: 'technicianCount',
      title: '技师数量',
      minWidth: 120,
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
